import { AppCTX } from "../../types/koaExtend";
import { createBrandApiMWRouter } from "../../middleware/brand-api/brandAdminChecker";
import {
  isNumber,
  isBoolean,
  _pick,
  isUndefined,
  StringIndexer,
  isEmpty,
  isInteger,
} from "mmb";
import fetch from "node-fetch";
import {
  imageUpload,
  applymentSubmit,
  applymentQuery,
} from "../../proxy/wxpayProxy";
import {
  checkWxpayApplyFormData,
  upsertWxpayApplyment,
  updateWxpayApplymentApplyNo,
  updateWxpayApplymentProxyResult,
  getWxpayApplymentAndRemindWxpay,
} from "../../service/wxpayApplymentService";
import { WxpayApplyFormData } from "../../service/declareTypes";
const router = createBrandApiMWRouter();

// 获取品牌活动详情(品牌管理视角)
router.get("/brd/:brandId/wxpayApplyment", async (ctx: AppCTX, next) => {
  const { brandId } = ctx.params;
  const { userId } = ctx;

  let wxpayApplyment = await getWxpayApplymentAndRemindWxpay(brandId);
  // let output: StringIndexer | undefined = undefined;
  // if (!isUndefined(wxpayApplyment) && !isEmpty(wxpayApplyment.applyNo)) {
  //   let oldApplyState = wxpayApplyment.applyState;
    
  //   wxpayApplyment = await applymentQuery(wxpayApplyment);
  //   if (oldApplyState !== wxpayApplyment.applyState) {
  //     // 状态更改,需要更新
  //     console.log("微信支付审核远端状态更改,需要更新...");
  //     wxpayApplyment = await updateWxpayApplymentProxyResult(wxpayApplyment);
  //   } else {
  //     console.log("微信支付审核远端状态与本地一致,无需更新");
  //   }
  //   output = wxpayApplyment;
  // }

  ctx.body = wxpayApplyment;
});

// 创建提交申请
router.post("/brd/:brandId/wxpayApplyment", async (ctx: AppCTX, next) => {
  const { userId } = ctx;
  const { brandId } = ctx.params;
  const {} = ctx.request.body;
  const applyFormData: WxpayApplyFormData = ctx.request.body.wxpayApplyFormData;

  checkWxpayApplyFormData(applyFormData);
  console.log("微信支付审核表单检测通过...");

  // 1、本地创建微信支付申请
  let upsertWxpayApplymentRet = await upsertWxpayApplyment({
    brandId,
    applyFormData,
  });

  let { wxpayApplymentId } = upsertWxpayApplymentRet.wxpayApplyment

  const applyNo = await applymentSubmit({
    sgId: upsertWxpayApplymentRet.sgId,
    bizApplyNo: wxpayApplymentId,
    ...applyFormData,
  });
  const wxpayApplyment = await updateWxpayApplymentApplyNo({
    wxpayApplymentId,
    applyNo,
  });

  ctx.body = wxpayApplyment;
});

// 更新提交申请
router.put(
  "/brd/:brandId/wxpayApplyment/:wxpayApplymentId",
  async (ctx: AppCTX, next) => {
    const { userId } = ctx;
    const { brandId, wxpayApplymentId } = ctx.params;
    const {} = ctx.request.body;
    const applyFormData: WxpayApplyFormData =
      ctx.request.body.wxpayApplyFormData;

    checkWxpayApplyFormData(applyFormData);
    console.log("微信支付审核表单检测通过...");

    // 1、本地创建微信支付申请
    let upsertWxpayApplymentRet = await upsertWxpayApplyment({
      brandId,
      applyFormData,
    });
    console.log("upsertWxpayApplyment ok...");
    const applyNo = await applymentSubmit(
      {
        sgId: upsertWxpayApplymentRet.sgId,
        bizApplyNo: wxpayApplymentId,
        ...applyFormData,
      },
      true
    );
    console.log("applymentSubmit retry ok...");
    let wxpayApplyment = await updateWxpayApplymentApplyNo({
      wxpayApplymentId,
      applyNo,
    });
    console.log("applymentSubmit updateWxpayApplymentApplyNo ok...");
    ctx.body = wxpayApplyment;
  }
);

// 上传资料图片
router.post("/brd/:brandId/wxpayApplyment/media", async (ctx: AppCTX, next) => {
  const { userId } = ctx;
  const { brandId } = ctx.params;
  const { image, imageType, url } = ctx.request.body;
  // const writeStream = await fetch(url)
  // const buffer = await writeStream.buffer()
  // const mediaId = await imageUpload(buffer, imageUpload)

  const mediaId = await imageUpload(image, imageType);

  ctx.body = {
    mediaId,
  };
});

export default router;
